package com.wn.class30.cotroller.houtai;

import com.wn.class30.entity.AdminUser;
import com.wn.class30.entity.Myorder;
import com.wn.class30.service.AfterSaleService;
import com.wn.class30.service.OrderDetailService;
import com.wn.class30.service.OrderService;
import com.wn.class30.service.ProductService;
import com.wn.class30.tools.Page;
import com.wn.class30.tools.Result;
import com.wn.class30.tools.StringToArray;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: OrderController 方法是
 * @author: jzy
 * @date: 2020/4/2 4:13 PM
 */
@RestController
@RequestMapping("houtai/page/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Resource
    private AfterSaleService afterSaleService;
    @Resource
    private ProductService productService;
    @Resource
    private OrderDetailService orderDetailService;

    /**
     * 根据条件分页查询订单信息
     * @param page
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    @RequiresPermissions("/houtai/page/order/list")
    public Result selectOrders(Integer page, Integer limit, String myorderNumber, String username, String orderStatus, String startDate,String endDate)throws Exception{
        log.info("myorderNumber======="+myorderNumber);
        log.info("username========"+username);
        log.info("orderStatus========"+orderStatus);
        log.info("startDate=========="+startDate);
        log.info("endDate=========="+endDate);
        Map<String,String> map = new HashMap<>();
        map.put("page",String.valueOf((page-1)*limit) );
        map.put("limit",String.valueOf(limit) );
        map.put("myorderNumber", myorderNumber);
        map.put("username", username);
        map.put("orderStatus", orderStatus);
        map.put("startDate", startDate);
        map.put("endDate", endDate);

        log.info("这是map中的myorderNumber==========="+map.get("myorderNumber"));
        //判断登陆用户是否为管理员，如果是管理员可以查询所有订单，非管理员只能查询对应商铺订单
        Subject subject = SecurityUtils.getSubject();
        AdminUser adminUser = (AdminUser) subject.getPrincipal();

        Long orderCount = orderService.getOrderCount(map);
        Page p = new Page();
        p.setCurrentPage((page-1)*limit);
        p.setTotalSize(orderCount);
        p.setPageSize(limit);
        long pageSize = orderCount % limit == 0? orderCount / limit:orderCount / limit + 1;
        p.setPageSize((int)pageSize);
        List<Myorder> myorders = orderService.selectOrders(map);
        for (Myorder myorder : myorders) {
            myorder.setUser(orderService.selectUserById(myorder.getUserId()));
            myorder.setUserAddress(orderService.selectUserAddressBy(myorder.getUserAddressId()));
        }
        return new Result("0","success",p,myorders);
    }


    /**
     * 批量删除或者单个删除订单
     * @param oid
     * @return
     * @throws Exception
     */
    @RequestMapping("delete")
    @RequiresPermissions("/houtai/page/order/delete")
    public Result deleteOrderByOid(String oid)throws Exception{
        log.info("oid=================="+oid);
        if(oid != null && !"".equals(oid)){
            Integer[] intOids = StringToArray.getArray(oid);
            for (Integer intOid : intOids) {
                orderService.deleteOrderByOid(intOid);
            }
            return new Result("200","success",null,null);
        }
        return new Result("500","error",null,null);
    }
}